home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 7661 / 7661.xpi / chrome / isreaditlater.jar / content / RILplaces.js < prev    next >
Text File  |  2009-10-15  |  5KB  |  142 lines

  1. // Scripts hacked together from:
  2. // http://mxr.mozilla.org/firefox/source/browser/components/places/content/controller.js#735
  3. // http://mxr.mozilla.org/firefox/source/browser/components/places/content/utils.js#890
  4.  
  5. function RILplaces()
  6. {
  7.     this.APP = Components.classes['@ril.ideashower.com/rildelegate;1'].getService().wrappedJSObject;    
  8. }
  9. RILplaces.prototype =
  10. {
  11.     init : function()
  12.     {
  13.         document.getElementById('placesContext').addEventListener("popupshowing", function() { return RILplaces.contextShowing() }, false);  
  14.     },
  15.     
  16.     saveView : function(view, event)
  17.     {try{
  18.         if (!this.APP)
  19.         {
  20.             Components.utils.reportError('Error saving bookmark folder: Could not activate Read It Later');
  21.             return;
  22.         }
  23.         
  24.         let urlsToOpen = [];
  25.         let node = view.selectedNode;
  26.         
  27.         if (node && PlacesUtils.nodeIsContainer(node))
  28.         {
  29.             urlsToOpen = this.getItemsForContainerNode(node);
  30.             return this.saveUrlsFromView(urlsToOpen, event);
  31.         }
  32.         
  33.         // else
  34.         let nodes = view.getSelectionNodes();
  35.         for (let i=0; i < nodes.length; i++)
  36.         {
  37.           // skip over separators and folders
  38.             if (PlacesUtils.nodeIsURI(nodes[i]))
  39.                 urlsToOpen.push({uri: nodes[i].uri, title:nodes[i].title, isBookmark: PlacesUtils.nodeIsBookmark(nodes[i])});
  40.         }
  41.         
  42.         return this.saveUrlsFromView(urlsToOpen, event);
  43.     }catch(e){Components.utils.reportError(e);}
  44.     },
  45.     
  46.     saveUrlsFromView : function(items, event)
  47.     {
  48.         for (var i = 0; i < items.length; i++)
  49.         {
  50.             var item = items[i];
  51.             if (item.isBookmark && item.uri)
  52.             {
  53.                 this.APP.LIST.add( {url:item.uri, title:item.title}, true );
  54.             }
  55.         }  
  56.         
  57.         this.APP.LIST.endBatchAndRefresh();                       
  58.     },
  59.     
  60.     // From getURLsForContainerNode
  61.     // http://mxr.mozilla.org/firefox/source/toolkit/components/places/src/utils.js#948
  62.     // added the node title to the item
  63.     
  64.     getItemsForContainerNode : function(aNode)
  65.     {
  66.         let urls = [];
  67.      if (PlacesUtils.nodeIsFolder(aNode) && asQuery(aNode).queryOptions.excludeItems) {
  68.        // grab manually
  69.        var itemId = PlacesUtils.getConcreteItemId(aNode);
  70.        let contents = PlacesUtils.getFolderContents(itemId, false, false).root;
  71.        for (let i = 0; i < contents.childCount; ++i) {
  72.          let child = contents.getChild(i);
  73.          if (PlacesUtils.nodeIsURI(child))
  74.            urls.push({uri: child.uri, title:child.title, isBookmark: PlacesUtils.nodeIsBookmark(child)});
  75.        }
  76.      }
  77.      else {
  78.        let result, oldViewer, wasOpen;
  79.        try {
  80.          let wasOpen = aNode.containerOpen;
  81.          result = aNode.parentResult;
  82.          oldViewer = result.viewer;
  83.          if (!wasOpen) {
  84.            result.viewer = null;
  85.            aNode.containerOpen = true;
  86.          }
  87.          for (let i = 0; i < aNode.childCount; ++i) {
  88.            // Include visible url nodes only
  89.            let child = aNode.getChild(i);
  90.            if (PlacesUtils.nodeIsURI(child)) {
  91.              // If the node contents is visible, add the uri only if its node is
  92.              // visible. Otherwise follow viewer's collapseDuplicates property,
  93.              // default to true
  94.              if ((wasOpen && oldViewer && child.viewIndex != -1) ||
  95.                  (oldViewer && !oldViewer.collapseDuplicates) ||
  96.                  urls.indexOf(child.uri) == -1) {
  97.                urls.push({ uri: child.uri, title:child.title,
  98.                            isBookmark: PlacesUtils.nodeIsBookmark(child) });
  99.              }
  100.            }
  101.          }
  102.          if (!wasOpen)
  103.            aNode.containerOpen = false;
  104.        }
  105.        finally {
  106.          if (!wasOpen)
  107.            result.viewer = oldViewer;
  108.        }
  109.      }
  110.  
  111.      return urls;
  112.  
  113.     },
  114.     
  115.     //http://mxr.mozilla.org/firefox/source/browser/components/places/content/controller.js#577
  116.     contextShowing : function()
  117.     { 
  118.         try {
  119.         var view = PlacesUIUtils.getViewForNode(document.popupNode);
  120.         var item = document.getElementById("RIL_saveFolder");
  121.         
  122.         if (!item.hidden && view.selectedNode &&
  123.             PlacesUtils.nodeIsContainer(view.selectedNode))
  124.         {
  125.             item.disabled = !PlacesUtils.hasChildURIs(view.selectedNode);
  126.         }
  127.         else
  128.         {
  129.             // see selectiontype rule in the overlay
  130.             item.disabled = item.hidden;
  131.         }
  132.         
  133.         item.parentNode.insertBefore( item, document.getElementById('placesContext_openSeparator'));
  134.         } catch(e){Components.utils.reportError(e);}
  135.         return;
  136.     }
  137. }
  138.  
  139. RILplaces = new RILplaces();
  140.  
  141.  
  142. window.addEventListener("load", function() {RILplaces.init();}, false);